<?php
namespace app\admin\model;

/*用户信息模型*/
use app\admin\model\CommonModel;

class UserInfo extends CommonModel
{
    private $table_name = 'user_info';
    public function __construct()
    {
       parent::__construct();
    }
    /**初始化参数
     * @param $args [client_id,token,enc_string,nickname]
     * @return array
     */
    private function default_param($args)
    {
        $default = [
            "client_id"     => "",
            "name" => "",
            "phone"      => "",
            "company"  => "",
            "remark"    => "",
            "add_time"  => ""
        ];
        return array_merge($default,$args);
    }


    //获取所有的用户基本信息
    public function all($args = [])
    {
        /**
         * $args参数说明
         * args.client_id 根据用户client_id
         * args.keyword 根据 name phone company与之匹配
         * args.start_time
         * args.end_time
         */

        $pageLimit = isset($args['pageLimit']) && intval($args['pageLimit']) > 0 ? $args['pageLimit'] : 15;
        $where = [];
        if(isset($args['client_id']) && !empty($args['client_id']))
        {
            $where['client_id'] = intval($args['client_id']);
        }

        if(isset($args['keyword']) && !empty($args['keyword']))
        {
            $where['name|phone|company'] = ['like','%'.$args['keyword'].'%'];
        }

        if(isset($args['start_time']) && !empty($args['start_time']))
        {
            $where['add_time'][] = ['>=',strtotime(trim($args['start_time']))];
        }

        if(isset($args['end_time']) && !empty($args['end_time']))
        {
            if(!empty($where['add_time']))
            {
                $where['add_time'][] = ['<=',strtotime(trim($args['end_time']))];
            }
            else
            {
                $where['add_time'][] = ['<=',strtotime(trim($args['end_time']))];

            }
        }
        //获取数据
        $data = null;
        if(!empty($where))
        {
            $data = db($this->table_name)->where($where)->order('id DESC')->paginate($pageLimit);
        }
        else
        {
            $data = db($this->table_name)->order('id DESC')->paginate($pageLimit);
        }
        if(!empty($data))
        return [
            'data' => $data,
            'page' => $data->render()
        ];
    }

    /**获取一个用户基本信息
     * @param array $args key:[id], 中括号表示任选一个可以查询
     * @return bool
     */
    public function info($args)
    {

        if(empty($args)) return false;
        //参数合并
        $args = $this->default_param($args);
        //如果没有查询信息，返回空
        if(!isset($args['id']) || empty($args['id'] )
        ) return false;

        //根据id,title,content其中一个查询
        if(!empty($args['id']))
        {
            $filter = ['id'=>$args['id']];
        }

        if(empty($filter)) return false;

        //返回查询信息
        return  db($this->table_name)->where($filter)->find();
    }

    //删除自动回复
    public function del($args)
    {
        /**
         * args参数说明
         * args为一个数组，包含所有的id值
         */
        if(empty($args))
        {
            return $this->returnState(1,"没有记录被删除");
        }
        db($this->table_name)->where(['id'=>['in',implode(',',$args)]])->delete();
        return $this->returnState(0,"删除完毕");
    }

    //编辑一个记录
    public function edit($args)
    {
        /**
         * args参数说明
         * args.id
         * args.name
         * args.phone
         * args.company
         * args.remark
         */

        //检查参数
        $args = $this->default_param($args);
        if(!isset($args['id']) || intval($args['id']) == 0)
        {
            return $this->returnState(1,"没有选中记录进行编辑");
        }

        //检查记录是否存在
        $info = $this->info(['id'=>intval($args['id'])]);
        if(!$info || empty($info))
        {
            return $this->returnState(1,"该用户信息不存在，或者已被删除");
        }

        $id = $info['id'];
        //合并两个数组
        $fields = ['name','phone','company','remark'];
        $update = array_merge(resetArr($info,$fields),resetArr($args,$fields));

        //删除空的项
        $update = array_filter($update);
        //信息更新
        db($this->table_name)->where(['id'=>$id])->update($update);

        return $this->returnState(0,"更新完毕");
    }


}
